﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApi.DbContexts;
using WebApi.Entities;

namespace WebApi.Controllers;

[ApiController]
[Route("[controller]")]
public class DemoController :Controller
{
    public DemoController()
    {
            
    }


    [HttpPost]
    [Route("add")]
    public ActionResult<int> AddDemo()
    {
        var context = new AppDbContext();

        var entity = new Demo();

        entity.Id = Guid.NewGuid();
        entity.Name = "Test";
        entity.Tags = new List<Tag>()
        {
            new Tag() {Value = "C++" },
            new Tag() {Value = "Swift" },
            new Tag() {Value = "C"}
        };



        context.Add(entity);
        return context.SaveChanges();

    }

    [HttpPut]
    [Route("update")]
    public ActionResult<int> UpdateTags()
    {
        var context = new AppDbContext();

        var demo = context.Demo.AsQueryable().FirstOrDefault();

        if (demo == null) throw new Exception("not found");

        demo.Tags.Add("python");
        
        return context.SaveChanges();
    }

   
    [Route("delete")]
    [HttpPut]
    public ActionResult<int> DeleteTags()
    {
        var context = new AppDbContext();

        var demo = context.Demo.AsQueryable().FirstOrDefault();

        if (demo == null) throw new Exception("not found");

        var t1 = demo.Tags.FirstOrDefault();
        demo.Tags.Remove(t1);
        
        return context.SaveChanges();
    }
}
